﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Member.ShippingAddresses.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.ShippingAddresses
{
    [AbpAuthorize(PermissionNames.Member)]
    public class MemberShippingAddressAppService : shenghuaAppServiceBase, IMemberShippingAddressAppService
    {
        private readonly MemberShippingAddressManager _memberShippingAddressManager;

        public MemberShippingAddressAppService(
            MemberShippingAddressManager memberShippingAddressManager
            )
        {
            _memberShippingAddressManager = memberShippingAddressManager;
        }

        public async Task<MemberShippingAddressDto> Create(CreateMemberShippingAddressDto input)
        {
            MemberShippingAddress memberShippingAddress = new MemberShippingAddress
            {
                PersonName = input.PersonName,
                PhoneNumber = input.PhoneNumber,
                Province = input.Province,
                City = input.City,
                County = input.County,
                DetailedAddress = input.DetailedAddress,
                IsDefault = input.IsDefault
            };
            await _memberShippingAddressManager.Add(MemberId, memberShippingAddress);
            return ObjectMapper.Map<MemberShippingAddressDto>(memberShippingAddress);
        }

        public async Task<MemberShippingAddressDto> Update(UpdateMemberShippingAddressDto input)
        {
            var memberShippingAddress = await _memberShippingAddressManager.Get(MemberId, input.Id);
            memberShippingAddress.PersonName = input.PersonName;
            memberShippingAddress.PhoneNumber = input.PhoneNumber;
            memberShippingAddress.Province = input.Province;
            memberShippingAddress.City = input.City;
            memberShippingAddress.County = input.County;
            memberShippingAddress.DetailedAddress = input.DetailedAddress;
            memberShippingAddress.IsDefault = input.IsDefault;
            await _memberShippingAddressManager.Update(MemberId, memberShippingAddress);
            return ObjectMapper.Map<MemberShippingAddressDto>(memberShippingAddress);
        }

        [HttpPost]
        public async Task Delete(EntityDto input)
        {
            await _memberShippingAddressManager.Delete(MemberId, input.Id);
        }

        public async Task<List<MemberShippingAddressDto>> GetAll()
        {
            var items = await _memberShippingAddressManager.GetAll(MemberId);
            return ObjectMapper.Map<List<MemberShippingAddressDto>>(items);
        }

        public async Task SetDefault(EntityDto input)
        {
            await _memberShippingAddressManager.SetDefault(MemberId, input.Id);
        }
    }
}
